#ifndef _HTTPSERVERCONNECTION_HPP_
#define _HTTPSERVERCONNECTION_HPP_

#include "jhdefs.hpp"
#include "net/HTTPResponse.hpp"
#include "net/TCPServerConnection.hpp"
#include "net/HTTPServerParams.hpp"
#include "net/HTTPRequestHandler.hpp"

NET_NAMESPACE_BEGIN
class CHTTPServerSession;
class CHTTPServerConnection : public CTCPServerConnection
{
public:
	CHTTPServerConnection(const CStreamSocket& socket, CHTTPServerParams::Ptr pParams, CHTTPRequestHandlerFactory::Ptr pFactory);
	virtual ~CHTTPServerConnection();

	void run();
	
protected:
	void sendErrorResponse(CHTTPServerSession& session, CHTTPResponse::HTTPStatus status);
	void onServerStopped(const bool& abortCurrent);

private:
	CHTTPServerParams::Ptr          m_pParams;
	CHTTPRequestHandlerFactory::Ptr m_pFactory;
	bool	m_stopped;
	CMutex	m_mutex;
};


class CHTTPServerConnectionFactory : public CTCPServerConnectionFactory
{
public:
	CHTTPServerConnectionFactory(CHTTPServerParams::Ptr pParams, CHTTPRequestHandlerFactory::Ptr pFactory);
	~CHTTPServerConnectionFactory();

	CTCPServerConnection* createConnection(const CStreamSocket& socket);

private:
	CHTTPServerParams::Ptr          m_pParams;
	CHTTPRequestHandlerFactory::Ptr m_pFactory;
};


NET_NAMESPACE_END

#endif
